Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
aurelia-typed-observable-plugin
Advanced tools
A plugin that provides enhanced @bindable
and @observable
decorators for Aurelia applications
@observable
, @bindable
@observable
with fluent syntax: @observable.custom
/ @observable.custom()
@observable.date
, @observable.number
, @observable.string
, @observable.boolean
, 5 for @bindable
with extra built-in: @bindable.booleanAttr
npm i aurelia-typed-observable-plugin
The decorators (@observable
and @bindable
) should be imported from this plugin, not the built in one of aurelia
, like the following:
// from Aurelia
import { bindable, observable } from 'aurelia-framework';
// from plugin
import { bindable, observable } from 'aurelia-typed-observable-plugin';
The two decorators are drop-in replacement for built-in decorators of Aurelia
, no extra work needed to use them beside importing them from this plugin.
This plugin is an attempt to request for comment from anyone who interested in the features feature. It was originally intended to be part of the core, but
there is concern it would be hard to support down the road. You can find original PR at aurelia-binding
(https://github.com/aurelia/binding/pull/623) and aurelia-templating
(https://github.com/aurelia/templating/pull/558)
// view model
export class VideoPlayer {
// do it yourself
@bindable({
coerce(val) {
if (val || val === '') {
return true;
}
return false;
}
})
playing
// or use built in
@bindable.booleanAttr
playing
}
All of the following, will be converted to true, which matches native behavior.
<!-- app.html -->
<template>
<video-player playing></video-player>
<video-player playing=''></video-player>
<video-player playing='true'></video-player>
<video-player playing='playing'></video-player>
<!-- instead of specifying command to make it a boolean -->
<video-player playing.bind='true'></video-player>
</template>
With normal syntax
class MyViewModel {
@observable({ coerce: 'number' }) numProp;
@observable({ coerce: 'boolean' }) boolProp;
@observable({ coerce: val => convertValue(val) }) customCoerceProp;
}
Using metadata
import {metadata} from 'aurelia-metadata';
class MyViewModel {
@observable
@Reflect.metadata(metadata.propertyType, Number)
num;
}
var instance = new MyViewModel();
instance.num = '4';
instance.num; // <====== 4
TypeScript users can achieve above result (metadata) by simpler code:
class MyViewModel {
@observable num: number;
}
var instance = new MyViewModel();
instance.num = '4';
instance.num; // <===== 4 , with compile error though
Enable emit decorator metadata, instruction: Instruction for the compiler to emit decorator metadata TypeScript decorator doc
Enable property type flag for each deorator, as by default, it is off to avoid breaking your code when opt-ed in, like following:
import {
usePropertyTypeForBindable,
usePropertyTypeForObservable
} from 'aurelia-typed-observable-plugin';
usePropertyTypeForBindable(true);
usePropertyTypeForObservable(true);
They are only needed to be called once.
All coerce type will be resolved to a string, which then is used to get the converter function in coerceFunctions
export of this module. So, to extend or modify basic implementations:
import {coerceFunctions} from 'aurelia-typed-observable-plugin';
// Modify built in
coerceFunctions.string = function(a) {
return a === null || a === undefined ? '' : a.toString();
}
// Extend
coerceFunctions.point = function(a) {
return a.split(' ').map(parseFloat).slice(0, 2);
}
// Usage of 'point' coerces defined above:
class MyLine {
@observable({ coerce: 'point' }) point1;
@observable({ coerce: 'point' }) point2;
}
For TS users or JS users who want to use metadata, to extend coerce mapping:
import {
createTypedObservable
} from 'aurelia-typed-observable-plugin';
// use static class method
class Point {
static coerce(value) {
return new Point(value);
}
}
mapCoerceFunction(Point, 'point');
// or just pass a 3rd parameter, fallback to static coerce method when 3rd param omitted:
mapCoerceFunction(Point, 'point', val => new Point(val));
// decorator usage:
// TypeScript
class MyLine {
@observable point1: Point
@observable point2: Point
}
// JavaScript
class MyLine {
@observable
@Reflect.metatata('design:type', Point)
point1
// or like this
@observable({ coerce: 'point' })
point2
}
class MyLine {
@observable.number x1
@observable.number() y1
@observable.number() x2
@observable.number y2
}
var line = new MyLine();
line.x1 = '15';
line.x1; // <======= 15
To built your own fluent syntax observable:
import {
coerceFunctions,
createTypedObservable
} from 'aurelia-typed-observable-plugin'
coerceFunctions.point = function(value) {
return value.split(' ').map(parseFloat);
}
createTypedObservable('point');
// usage:
class MyLine {
@observable.point point1;
@observable.point point2;
}
Special thanks to Fred Kleuver for his plugin skeleton at https://github.com/aurelia-contrib/aurelia-plugin-skeleton-ts-webpack
FAQs
A plugin for @observable and @bindable value coercion
The npm package aurelia-typed-observable-plugin receives a total of 1,404 weekly downloads. As such, aurelia-typed-observable-plugin popularity was classified as popular.
We found that aurelia-typed-observable-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.